package com.cindata

import grails.transaction.Transactional

@Transactional
class RiskWarningFlowStageRoleService
{
    def initFlowStageRole(RiskWarning riskWarning, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.roles?.each {
                def riskWarningFlowStageRole = new RiskWarningFlowStageRole()
                riskWarningFlowStageRole.role = it?.role
                riskWarningFlowStageRole.teamRole = it?.teamRole
                riskWarningFlowStageRole.flowStage = RiskWarningFlowStage.findByRiskWarningAndStage(riskWarning, workflowStage?.stage)
                if (riskWarningFlowStageRole.validate())
                {
                    riskWarningFlowStageRole.save flush: true
                }
                else
                {
                    println riskWarningFlowStageRole.errors
                }
            }
        }
    }
}